package cn.shujuhai.qtadmin.platform.service.impl;

import java.io.File;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import cn.shujuhai.qtadmin.platform.service.SendEmailService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dch
 */
@Slf4j
@Service
public class SendEmailServiceImpl implements SendEmailService {
    private final JavaMailSender javaMailSender;

    public SendEmailServiceImpl(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public static void mailInfo(String from, String to, String cc, String subject, String text,
        JavaMailSender javaMailSender) {
        if (StringUtils.isBlank(to)) {
            return;
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to.split(","));
        if (StringUtils.isNotBlank(cc)) {
            message.setCc(cc.split(","));
        }
        message.setSubject(subject);
        message.setText(text);
        javaMailSender.send(message);
    }

    public static void sendAttachmail(String from, @NotNull String to, String cc, String subject, String text,
        File file, @NotNull JavaMailSender javaMailSender) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo(to.split(","));
        if (StringUtils.isNotBlank(cc)) {
            helper.setCc(cc.split(","));
        }

        helper.setSubject(subject);
        helper.setText(text);
        helper.addAttachment(file.getName(), file);

        javaMailSender.send(mimeMessage);
    }

    public static void sendMailFiles(String from, @NotNull String to, String cc, String subject, String text,
        List<File> attachments, @NotNull JavaMailSender javaMailSender) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo(to.split(","));
        if (StringUtils.isNotBlank(cc)) {
            helper.setCc(cc.split(","));
        }
        helper.setSubject(subject);
        helper.setText(text);
        for (File attachment : attachments) {
            helper.addAttachment(attachment.getName(), attachment);
        }
        javaMailSender.send(mimeMessage);
    }

    @Override
    @Async
    public void asyncSendEmail(String from, String to, String cc, String subject, String text) {
        mailInfo(from, to, cc, subject, text, javaMailSender);
    }

    /**
     * @param from
     *            from
     * @param to
     *            to
     * @param cc
     *            cc
     * @param subject
     *            subject
     * @param text
     *            text
     * @param file
     *            file
     */
    @SneakyThrows
    @Override
    @Async
    public void sendAttachmentMail(String from, @NotNull String to, String cc, String subject, String text, File file) {
        sendAttachmail(from, to, cc, subject, text, file, javaMailSender);

    }

    /**
     * @param from
     *            from
     * @param to
     *            to
     * @param cc
     *            cc
     * @param subject
     *            subject
     * @param text
     *            text
     * @param attachments
     *            attachments
     */
    @SneakyThrows
    @Override
    @Async
    public void sendAttachmentMail(String from, @NotNull String to, String cc, String subject, String text,
        List<File> attachments) {
        sendMailFiles(from, to, cc, subject, text, attachments, javaMailSender);
    }
}
